# Contributor: Chris Kruger <alpine@krugerheavyindustries.com>
# Maintainer: Chris Kruger <alpine@krugerheavyindustries.com>
pkgname=vaultwarden
pkgver=1.25.0
pkgrel=0
_pkgcommit=887e320e7f8dfc62d9b3ed08aca216cd7ad229f1
pkgdesc="bitwarden alternative backend"
_bw_web_ver=2022.05.0
provides="bitwarden_rs=$pkgver-r$pkgrel"
replaces="bitwarden_rs"
url="https://github.com/dani-garcia/vaultwarden"
arch="all !ppc64le !s390x !riscv64" # ring crate has platform specific asm - no ppc support, no rust on s390x
license="GPL-3.0-only"
makedepends="rust cargo sqlite-dev libpq-dev mariadb-dev openssl-dev"
subpackages="$pkgname-doc $pkgname-openrc"
install="$pkgname.pre-install"
pkgusers="vaultwarden"
pkggroups="vaultwarden"
source="$pkgname-$pkgver.tar.gz::https://github.com/dani-garcia/vaultwarden/archive/$_pkgcommit.tar.gz
	$pkgname-webvault-$_bw_web_ver.tar.gz::https://github.com/dani-garcia/bw_web_builds/releases/download/v$_bw_web_ver/bw_web_v$_bw_web_ver.tar.gz
	$pkgname.initd
	$pkgname.confd"
builddir="$srcdir/vaultwarden-$_pkgcommit"

prepare() {
	default_prepare
	cargo fetch --locked
}

build() {
	cat >> Cargo.toml <<- EOF
		codegen-units = 1
		opt-level = "s"
		panic = "abort"
	EOF
	cargo build --release --features sqlite,postgresql,mysql,enable_mimalloc --frozen
}

check() {
	cargo test --release --features sqlite,postgresql,mysql,enable_mimalloc --frozen
}

package() {
	install -d "$pkgdir"/usr/sbin
	install -m755 target/release/vaultwarden "$pkgdir"/usr/sbin/
	install -d -m744 -o vaultwarden -g vaultwarden "$pkgdir"/var/lib/vaultwarden "$pkgdir"/var/log/"$pkgname"
	(cd "$srcdir"/web-vault; find . -type f -exec \
		install -Dm644 -o vaultwarden -g vaultwarden {} "$pkgdir"/var/lib/vaultwarden/web-vault/{} \;)
	(cd "$builddir"/src/static; find . -type f -exec \
		install -Dm644 -o vaultwarden -g vaultwarden {} "$pkgdir"/var/lib/vaultwarden/{} \;)
	install -Dm644 README.md "$pkgdir"/usr/share/doc/vaultwarden/README.md
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm655 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
}

sha512sums="
e11edc96e009b44affe12329322b47c30b273d0c331a721b42a97e77a8fff4c25f39716e839a07a6a6d315203d02bf26ab0d7441e9a3790d7ebfded232be1857  vaultwarden-1.25.0.tar.gz
de0b701f9eba6407dc74b2081cf4bfde5c79035c66c984118103250aa43c1d663c7f4895818c4cabbd4a0355b2104921090ad10bbabc94a5ba9a206ba4809657  vaultwarden-webvault-2022.05.0.tar.gz
c15bbe6b21ab576c7532e1e2781068d99ef163a84a93414db0246113730e1cc058b6007ea2129d81c678aae035fd91be0e8c031f25de24b826a92d4ce8999aa3  vaultwarden.initd
115cfa4ca84cdac41736b366a2c5e729adf5a5a8499c8a06dfd9d1544b64a365d6fbc3ceeb3aef54141aa29694e7054f03dd9589092c46be32d94e3537167e16  vaultwarden.confd
"
